package com.jrx.anytxn.transaction.job.coupon.config;

import com.alibaba.fastjson.JSONArray;
import com.jrx.anytxn.common.constant.Constants;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.customer.entity.CmCustomerSecondLevel;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.dto.common.SendSmsReq;
import com.jrx.anytxn.transaction.service.IActivityLoanService;
import com.jrx.anytxn.transaction.service.ISmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

/**
 * ClassName CouponHalfActivitySmsRemindConfiguration
 *
 * @author zebin.zhang
 * @version 1.0
 * @description 抢五折券活动开始短信提醒
 * @date 2022/02/07 17:06
 **/

@Configuration
public class CouponHalfActivitySmsRemindConfiguration {
    private static final Logger logger =
            LoggerFactory.getLogger(CouponHalfActivitySmsRemindConfiguration.class);
    private static final BigDecimal SUM_AMOUNT = new BigDecimal("299");

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private IActivityLoanService activityLoanService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISmsService smsService;

    @Bean
    public Job halfActivitySmsRemindJob() {
        return jobBuilderFactory.get("halfActivityStoreQuantityWeekendJob")
                .start(halfActivitySmsRemindStep()).build();
    }

    @Bean
    public Step halfActivitySmsRemindStep() {
        return stepBuilderFactory.get("halfActivityStoreQuantityWeekendStep")
                .tasklet(halfActivitySmsRemindSTasklet()).build();
    }

    @Bean
    @StepScope
    public Tasklet halfActivitySmsRemindSTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("access into halfActivitySmsRemindSTasklet");
            String startTime = DateUtils.getDayOfWeek(Calendar.MONDAY) + " 00:00:00";
            String endTime = DateUtils.getDayOfWeek(Calendar.THURSDAY) + " 23:59:59";
            List<String> customerIdList = activityLoanService.getAmLoanMainInfoByConsumeAmountWeekly(startTime, endTime, TransBizConstant.LOAN_TYPE_4, SUM_AMOUNT, Constants.DEFAULT_PRODTCT_CODE);
            logger.info("---------------" + JSONArray.toJSONString(customerIdList));
            if (null != customerIdList && customerIdList.size() > 0) {
                SendSmsReq req = new SendSmsReq();
                req.setChannel(Constants.DEFAULT_CHANNEL);
                req.setTenantId(Constants.DEFAULT_TENANT_ID);
                req.setSmsType(TransBizConstant.SMS_TYPE_HALF_ACTIVITY_REMIND);
                for (String customerId : customerIdList) {
                    CmCustomerSecondLevel customerSecondLevel = customerService.getSecondByChannelAndCustomerIdAndTensnId(Constants.DEFAULT_CHANNEL,
                            customerId, Constants.DEFAULT_TENANT_ID, null);
                    if (null != customerSecondLevel) {
                        logger.info("----组装参数中----");
                        //生成流水号
                        String currentTimeMillis = System.currentTimeMillis() + "";
                        String seqNo = (int) (Math.random() * 100000) + currentTimeMillis;
                        req.setSeqNo(seqNo);

                        String mobile = customerSecondLevel.getMobile();
                        req.setMobile(mobile);
                        req.setCustomerId(customerId);
                        smsService.sendSmsMq(req);
                    }
                }
            }

            return RepeatStatus.FINISHED;
        };
    }
}
